package com.panghl.report;

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * @Author panghl
 * @Date 2022/2/27
 * @Description 导出带样式的excel
 **/
public class POIAndStyleDemo {
    public static void main(String[] args) throws IOException {
        Workbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet("带样式的sheet");
        //列宽
        sheet.setColumnWidth(0, 5 * 256);
        sheet.setColumnWidth(1, 6 * 256);
        sheet.setColumnWidth(2, 7 * 256);
        sheet.setColumnWidth(3, 8 * 256);
        sheet.setColumnWidth(4, 9 * 256);
        //1. 边框线
        CellStyle bigTitleRowCellStyle = workbook.createCellStyle();
        bigTitleRowCellStyle.setBorderBottom(BorderStyle.THIN); // THIN 细线
        bigTitleRowCellStyle.setBorderLeft(BorderStyle.THIN);
        bigTitleRowCellStyle.setBorderRight(BorderStyle.THIN);
        bigTitleRowCellStyle.setBorderTop(BorderStyle.THIN);
        // 对齐方式： 水平对齐，垂直对齐
        bigTitleRowCellStyle.setAlignment(HorizontalAlignment.CENTER);
        bigTitleRowCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        //创建字体
        Font font = workbook.createFont();
        font.setFontName("黑体");
        font.setFontHeightInPoints((short) 18);
        //把字体放入到样式中
        bigTitleRowCellStyle.setFont(font);
        Row bigTitleRow = sheet.createRow(0);
        //2. 行高
        bigTitleRow.setHeightInPoints(42);
        for (int i = 0; i < 5; i++) {
            Cell cell = bigTitleRow.createCell(i);
            cell.setCellStyle(bigTitleRowCellStyle);
        }
        //3. 合并单元格
        sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 4));
        //向单元格中放入一句话
        sheet.getRow(0).getCell(0).setCellValue("用户表信息");


        //小标题的样式
        CellStyle littleTitleRowCellStyle = workbook.createCellStyle();
        //样式的克隆
        littleTitleRowCellStyle.cloneStyleFrom(bigTitleRowCellStyle);
        // 对齐方式： 水平对齐，垂直对齐
        littleTitleRowCellStyle.setAlignment(HorizontalAlignment.CENTER);
        littleTitleRowCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        //创建字体
        Font littleFont = workbook.createFont();
        littleFont.setFontName("宋体");
        littleFont.setFontHeightInPoints((short) 12);
        littleFont.setBold(true);
        //把字体放入到样式中
        littleTitleRowCellStyle.setFont(littleFont);


        //内容的样式
        CellStyle contentRowCellStyle = workbook.createCellStyle();
        //样式的克隆
        contentRowCellStyle.cloneStyleFrom(littleTitleRowCellStyle);
        contentRowCellStyle.setAlignment(HorizontalAlignment.CENTER);
        //创建字体
        Font contentFont = workbook.createFont();
        contentFont.setFontName("宋体");
        contentFont.setFontHeightInPoints((short) 11);
        contentFont.setBold(false);
        //把字体放入到样式中
        contentRowCellStyle.setFont(contentFont);

        Row titleRow = sheet.createRow(1);
        titleRow.setHeightInPoints(30);
        String[] titles = new String[]{"编号","姓名","手机号","入职日期","现住址"};
        for (int i = 0; i < 5; i++) {
            Cell cell = titleRow.createCell(i);
            cell.setCellValue(titles[i]);
            cell.setCellStyle(littleTitleRowCellStyle);
        }

        Row contentRow = sheet.createRow(2);
        String[] contents = new String[]{"1","panghl","159*****4233","2020-02-02","成都"};
        for (int i = 0; i < 5; i++) {
            Cell cell = contentRow.createCell(i);
            cell.setCellValue(contents[i]);
            cell.setCellStyle(contentRowCellStyle);
        }

        workbook.write(new FileOutputStream("D:\\testStyle.xlsx"));
    }
}
